library(magrittr)
library(stringr)
library(plyr)
library(tidyverse)
library(stargazer)


rm(list=ls())
home = 'C:/Users/jdt34/Dropbox/VNA_Responsiveness/Short Article/JOP-dataverse/'


permutation.results = paste0(home, 'Data/RI-analyses.Rds') %>%
  readRDS %>%
  subset(Result %in% c('tab2col3','tab3col3','tab3col4','tab3col6'),
         select=c(term, estimate, Result, N, Iteration.Analysis)) %>%
  mutate(Result=mapvalues(x=Result,
                          from=c('tab2col3','tab3col3','tab3col4','tab3col6'),
                          to=c('Survey','Pooled','Floor','Caucus')),
         Result=factor(x=Result,
                       levels=c('Survey','Pooled','Floor','Caucus')),
         term=factor(x=term,
                     levels=c('Citizen','Prop.Citizen','Citizen:Prop.Citizen',
                              'Firm','Prop.Firm','Firm:Prop.Firm',
                              'FullTime','CentNom','Competitive','(Intercept)'))) %>%
  set_colnames(c('Term','Estimate','Outcome','N','ID'))


experimental.results = paste0(home, 'Data/experimental-analyses.Rds') %>%
  readRDS %>%
  subset(Result %in% c('tab2col3','tab3col3','tab3col4','tab3col6'),
         select=c(term, estimate, Result, N, Iteration.Analysis)) %>%
  mutate(Result=mapvalues(x=Result,
                          from=c('tab2col3','tab3col3','tab3col4','tab3col6'),
                          to=c('Survey','Pooled','Floor','Caucus')),
         Result=factor(x=Result,
                       levels=c('Survey','Pooled','Floor','Caucus')),
         term=factor(x=term,
                     levels=c('Citizen','Prop.Citizen','Citizen:Prop.Citizen',
                              'Firm','Prop.Firm','Firm:Prop.Firm',
                              'FullTime','CentNom','Competitive','(Intercept)'))) %>%
  set_colnames(c('Term','Estimate','Outcome','N','ID'))


tableA06.1 = permutation.results %>%
  ddply(.variables=c('Outcome','Term'),
        .fun=function(x) {
          experimental.estimate = subset(experimental.results, 
                                         Outcome==x$Outcome[1] & Term==x$Term[1])$Estimate
          RI.pvalue = mean(experimental.estimate>x$Estimate)
          data.frame(Estimate=round(experimental.estimate, 3),
                     Pvalue=round(RI.pvalue, 3))
        },
        .inform=T) %>%
  ddply(.variables='Term',
        .fun=function(x) {
          data.frame(Term=c(as.character(x$Term[1]), ''),
                     Survey=c(str_pad(string=x$Estimate[1], width=5, side='right', pad=0), 
                              paste0('(', str_pad(string=x$Pvalue[1], width=5, side='right', pad=0), ')')),
                     Pooled=c(str_pad(string=x$Estimate[2], width=5, side='right', pad=0), 
                              paste0('(', str_pad(string=x$Pvalue[2], width=5, side='right', pad=0), ')')),
                     Floor=c(str_pad(string=x$Estimate[3], width=5, side='right', pad=0), 
                             paste0('(', str_pad(string=x$Pvalue[3], width=5, side='right', pad=0), ')')),
                     Caucus=c(str_pad(string=x$Estimate[4], width=5, side='right', pad=0), 
                              paste0('(', str_pad(string=x$Pvalue[4], width=5, side='right', pad=0), ')')))
        },
        .inform=T) %>%
  rbind(data.frame(Term='Observations',
                   Survey=subset(experimental.results,
                                 Outcome=='Survey')$N[1],
                   Pooled=subset(experimental.results,
                                 Outcome=='Pooled')$N[1],
                   Floor=subset(experimental.results,
                                Outcome=='Floor')$N[1],
                   Caucus=subset(experimental.results,
                                 Outcome=='Caucus')$N[1]))


stargazer(tableA06.1, 
          summary=F, 
          header=F,
          title='Tabular results for Paper Figures 1 and 2.', 
          align=T, 
          no.space=T, 
          rownames=F,
          label='tab:tabular',
          notes='Randomization inference based on 10,000 randomizations.\nQuantile of experimental estimate in parentheses.') %>%
  writeClipboard

